package com.spring.rabbitmq.workqueue;

import com.rabbitmq.client.*;
import com.spring.rabbitmq.utils.RabbitMQUtils;
import lombok.SneakyThrows;

import java.io.IOException;

/**
 * @Author: lzx
 * @Data: 2021/04/27/10:53
 * @Description：
 */
public class CustomerA {
    @SneakyThrows
    public static void main(String[] args) {
        Connection connection = RabbitMQUtils.getConnection();
        final Channel channel = connection.createChannel();
        channel.basicQos(1);//每次只消费一个消息
        channel.queueDeclare("work", true, false, false, null);
        channel.basicConsume("work", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者A: " + new String(body));
                //手动确认 参数1：手动确认消息标识 确认队列中的哪个具体的消息 参数2：是否开启多个消息同时确认 false 每次确认一次
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });

    }
}
